<template>
  <div id="stackBar">
    <div ref="chart01" class="chart01"></div>
  </div>
</template>

<script>
export default {
  name: "stackBar",
  components: {},
  props: {
    xData: Array | Object,
    y1Data: Array | Object,
    series1: Array | Object,
    series2: Array | Object,
    series3: Array | Object,
    series4: Array | Object,
    series5: Array | Object,
    name1: String,
    name2: String,
    whether: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {};
  },
  computed: {},
  watch: {
    xData: function(newVal,oldVal){
      this.drawStackBar()
    },
    y1Data: function(newVal,oldVal){
      this.drawStackBar()
    }
  },
  created() {},
  mounted() {
    this.drawStackBar();
  },

  methods: {
    drawStackBar() {
      let that = this;
      let mychart = that.$echarts.init(that.$refs.chart01);
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "line", // 默认为直线，可选为：'line' | 'shadow'
            lineStyle: {
              color: "#e5e5e5"
            }
          },
          extraCssText: 'z-index: 9', //设置消息提示框的层级
          formatter: function (params) {
            let result01 = "";
            params.forEach(function (item) {
              result01 +=
                item.marker +
                " " +
                item.seriesName +
                " : " +
                that.format_num(item.value) +
                "</br>";
            });
            return params[0].name + "</br>" + result01;
          },
        },
        grid: {
          top: '40',
          left: "40",
          right: "42",
          bottom: "50",
          containLabel: false,  //是否根据去掉图例改变图表的宽度
        },
        xAxis: [
          {
            type: "category",
            data: that.xData,
            //设置网格线颜色
            splitLine: {
              show: false,
              lineStyle: {
                color: ["#e5e5e5"],
                width: 1,
                type: "dashed",
              },
            },
            axisTick: {
                show: false
            },
            // x轴样式
            axisLine: {
              lineStyle: {
                type: "solid",
                color: "#e5e5e5", //左边线的颜色
                width: "1", //坐标线的宽度
              },
            },
            // x轴字体样式
            axisLabel: {
            //   interval: 0,
              // rotate: 30,
              margin: 14,
              show: true,
              textStyle: {
                color: "#888",
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12,
              },
              formatter: this.whether ? "{value}:00" :"{value}",
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: this.name2,          
            splitNumber: '3',
            nameTextStyle: {
              color: '#888',
              padding: [0, 0, 0, 48]
            },
            minInterval : 1,
            // boundaryGap : [ 0, 2],
            //设置网格线颜色
            splitLine: {
              show: true,
              lineStyle: {
                color: ["#e5e5e5"],
                width: 1,
                type: "dashed",
              },
            },
            axisTick: {
                show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#888",
                // fontFamily: "DINAlternate-Bold",
                fontSize: 12,
              },
              formatter: function (value) {
                return that.format_num(value);
              },
            },
            axisLine: {
              lineStyle: {
                type: "solid",
                color: "#e5e5e5", //左边线的颜色
                width: "1", //坐标线的宽度
              },
            },
          },
        ],
        dataZoom: [
          {
            type: 'slider',
            show: true,
            height: 22,
            bottom: 0,
            realtime: true,
            borderColor: 'transparent',
            // backgroundColor: '#f0935e',
            fillerColor: '#f0935e',
            // 拖拽手柄样式 svg 路径
            // handleIcon: 'M512 512m-208 0a6.5 6.5 0 1 0 416 0 6.5 6.5 0 1 0-416 0Z M512 192C335.264 192 192 335.264 192 512c0 176.736 143.264 320 320 320s320-143.264 320-320C832 335.264 688.736 192 512 192zM512 800c-159.072 0-288-128.928-288-288 0-159.072 128.928-288 288-288s288 128.928 288 288C800 671.072 671.072 800 512 800z',
            handleIcon: 'M0,0 v9.7h5 v-9.7h-5 Z',
            handleColor: '#fff',
            handleSize: 17,
            handleStyle: {
              color:"#294b97",
              borderColor:'rgba(240, 147, 94, 0.4)',
              borderSize:1,
              shadowBlur: 5,
              shadowColor: 'rgba(240, 147, 94, 0.8)',
              shadowOffsetX: 0, // 阴影偏移x轴多少
              shadowOffsetY: 0 // 阴影偏移y轴多少
            },
            start: 0,
            end: 100,
            textStyle:{
              color:'#f0935e',
            }
          },
        ],
        series: [
          {
            name: this.name1,
            type: "bar",
            // stack: "数量",
            barGap: "-100%" /*柱子重叠*/,
            // yAxisIndex: 0,
            data: that.series1,
            itemStyle: {
              normal: {
                // color: "#1779ff",
                color: '#f4b477'
                //柱子颜色
              },
            },
            barWidth:10, // 控制柱子的宽度
          },
        ],
      };
      mychart.setOption(option);
      //   窗口自适应
      setTimeout(() => {
        window.addEventListener("resize", function () {
          mychart.resize();
        });
      }, 200);
    },
  },
};
</script>
<style lang='less' scoped>
#stackBar {
  width: 100%;
  height: 100%;
  // margin-top: 36px;
  .chart01 {
    width: 100%;
    height: 100%;
    overflow: hidden;
    clear: both;
  }
}
</style>